package com.common.dao.util;

import java.util.List;
import java.util.logging.Logger;

public class QueryBuilder {
	
	private static final String SELECT_QUERY = "SELECT FROM";
	private static final String WHERE = "WHERE";
	private static final String ORDER_BY = "ORDER BY";
	private static final String AND = " AND";
	private static final String OBJECT = "obj";
	private static final String DOT = ".";
	private static final String COMMA = "\"";
	private static final String SPACE = " ";
	
	private QueryBuilder() {}
	
	public static String createCriteriaOrderQuery(List<Criteria> criterias, List<Order> orders, String table) {
		StringBuilder builder = new StringBuilder();
		
		builder.append(SELECT_QUERY);
		builder.append(SPACE);
		builder.append(table);
		builder.append(SPACE);
		builder.append(OBJECT);
		builder.append(SPACE);
		builder.append(createCriteriaList(criterias));
		builder.append(SPACE);
		builder.append(createOrderList(orders));
		
		Logger.getAnonymousLogger().severe(builder.toString());
		return builder.toString();
	}
	
	public static String createCriteriaQuery(List<Criteria> criterias, String table) {
		StringBuilder builder = new StringBuilder();
		
		builder.append(SELECT_QUERY);
		builder.append(SPACE);
		builder.append(table);
		builder.append(SPACE);
		builder.append(OBJECT);
		builder.append(SPACE);
		builder.append(createCriteriaList(criterias));
		
		Logger.getAnonymousLogger().severe(builder.toString());
		return builder.toString();
	}
	
	public static String createOrderQuery(List<Order> orders, String table) {
		StringBuilder builder = new StringBuilder();
		
		builder.append(SELECT_QUERY);
		builder.append(SPACE);
		builder.append(table);
		builder.append(SPACE);
		builder.append(OBJECT);
		builder.append(SPACE);
		builder.append(createOrderList(orders));
		
		Logger.getAnonymousLogger().severe(builder.toString());
		return builder.toString();
	}
	
	private static String createCriteriaList(List<Criteria> criterias) {
		StringBuilder builder = new StringBuilder();
		builder.append(WHERE);
		
		for (int i = 0; i < criterias.size();) {
			builder.append(SPACE);
			builder.append(createCriteria(criterias.get(i)));
			
			if (++i < criterias.size()) {
				builder.append(SPACE);
				builder.append(AND);
			}
		}
		
		return builder.toString();
	}
	
	private static String createOrderList(List<Order> orders) {
		StringBuilder builder = new StringBuilder();
		builder.append(ORDER_BY);
		
		for (int i = 0; i < orders.size();) {
			builder.append(SPACE);
			builder.append(createOrder(orders.get(i)));
			
			if (++i < orders.size()) {
				builder.append(SPACE);
				builder.append(AND);
			}
		}
		
		return builder.toString();
	}

	private static String createCriteria(Criteria criteria) {
		StringBuilder builder = new StringBuilder();
		
		builder.append(OBJECT);
		builder.append(DOT);
		builder.append(criteria.getTitle());
		builder.append(SPACE);
		builder.append(criteria.getOperation().getValue());
		builder.append(SPACE);
		
		if (criteria.hasComma()) {
			builder.append(COMMA);
			builder.append(criteria.getValue());
			builder.append(COMMA);
		} else {
			builder.append(criteria.getValue());
		}
		
		return builder.toString();
	}
	
	private static String createOrder(Order order) {
		StringBuilder builder = new StringBuilder();
		
		builder.append(OBJECT);
		builder.append(DOT);
		builder.append(order.getTitle());
		builder.append(SPACE);
		builder.append(order.getValue());
		
		return builder.toString();
	}
}
